<?php Yii::app()->clientScript->registerScriptFile(Yii::app()->params['baseAddress'] . 'script/jquery.format.js'); ?>
<?php Yii::app()->clientScript->registerScriptFile(Yii::app()->params['baseAddress'] . 'script/template.js'); ?>
<div class="form">
    <?php
    $form = $this->beginWidget('CActiveForm', array(
        'id' => 'galleria-form',
        'enableAjaxValidation' => false,
        'htmlOptions' => array('enctype' => 'multipart/form-data'),
            ));
    ?>
    <script type="text/javascript">
        $(document).ready(function(){
            CheckFileUploader();
            $("#Galleria_idTipoGalleria").change(function(){
                CheckFileUploader();
            });
            CheckMusicSource();
            $("#TipoFonte").change(function(){
                CheckMusicSource();
            });
            
//            $("#AddLink").click(function(){
//                var table = $("#linkTable");
//                //var append="<tr><td>aaaa</td><td>bbbbb</td><td>ccccc</td></tr>";
//                var append = '<tr><td><?php //echo $form->textField(Video::model(), 'title', array('size' => 60, 'maxlength' => 500)); ?></td>'+
//                    '<td><?php //echo $form->textField(Video::model(), 'link', array('size' => 60, 'maxlength' => 500)); ?></td>'+
//                    '<td><?php //echo $form->textField(Video::model(), 'descr', array('size' => 60, 'maxlength' => 500)); ?></td></tr>';
//                table.append(append);
//            });
            $("a#addTextLink").click(function(){
                AddLink();
            });
        });
        function CheckFileUploader()
        {
            <?php
            if (!$model->isNewRecord) 
            {
            ?>
                var selected = '<?php echo $model->Tipo->descr; ?>';
            <?php
            } 
            else 
            {
            ?>
                var selected = $("#Galleria_idTipoGalleria option:selected").text();
            <?php
            }
            ?>
            if(selected == "Immagini")
            {
                $("#immagini").show();
                $("#musica").hide();
                $("#video").hide();
                $("#letteratura").hide();
            }
            else if (selected == "Musica")
            {
                $("#immagini").hide();
                $("#musica").show();
                $("#video").hide();
                $("#letteratura").hide();
            }
            else if (selected == "Video")
            {
                $("#immagini").hide();
                $("#musica").hide();
                $("#video").show();
                $("#letteratura").hide();
            }
            else if (selected == "Letteratura")
            {
                $("#immagini").hide();
                $("#musica").hide();
                $("#video").hide();
                $("#letteratura").show();
            }            
        }
        function CheckMusicSource()
        {
            var selected = $("#Galleria_idTipoGalleria option:selected").text();
            if (selected == "Musica")
            {
                var optionselected = $("#TipoFonte option:selected").text();
                if(optionselected == 1)
                {
                    $("#mp3File").show();
                    $("#souncloudLink").hide();
                }
                if(optionselected == 2)
                {
                    $("#souncloudLink").show();
                    $("#mp3File").hide();
                }
            }
            else
            {
                $("#mp3File").hide();
                $("#souncloudLink").hide();
            }
        }
        
        function AddLink()
        {
            var idx = $("link_inputs").children().lenght + 1;
            $("link_inputs").append("<div><input type='text' id='txt_link_"+idx+"'/><input type='text' id='txt_descr_"+idx+"'/></div>");
            /*<div><input type="text" id="txt_link_1"/><input type="text" id="txt_descr_1"/></div>*/
        }
        
    </script>
    
    
    <p class="note">Fields with <span class="required">*</span> are required.</p>
<?php
$autore = Autore::model()->findByPk($model->IsNewRecord ? $_GET['idAutore'] : $model->idAutore);
$links = array();
foreach ($autore->Gallerie as $galleria) {
    if ($galleria->Video != null)
        $links = $galleria->Video;
}
?>
    <?php echo $form->errorSummary($model); ?>
    <div class="row">
    <?php echo $form->labelEx($model, 'idAutore'); ?>
    <?php echo $autore->nome; ?>
    <?php echo $form->error($model, 'idAutore'); ?>
    </div>
    <div class="row">
    <?php echo $form->labelEx($model, 'title'); ?>
    <?php echo $form->textField($model, 'title', array('size' => 60, 'maxlength' => 500)); ?>
    <?php echo $form->error($model, 'title'); ?>
    </div>
    <div class="row">
        <?php echo $form->labelEx($model, 'idTipoGalleria'); ?>
        <?php
        if ($model->isNewRecord)
            echo $form->dropDownList($model, 'idTipoGalleria', CHtml::listData(TipoGalleria::model()->findAll(), 'id', 'descr'));
        else
            echo $model->Tipo->descr;
        ?>
<?php echo $form->error($model, 'idTipoGalleria'); ?>
    </div>
    <div class="row">
        <?php echo $form->labelEx($model, 'description'); ?>
        <?php echo $form->textArea($model, 'description', array('size' => 60, 'maxlength' => 4000)); ?>
        <?php echo $form->error($model, 'description'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model, 'Path'); ?>
            <?php echo $form->textField($model, 'Path', array('size' => 60, 'maxlength' => 500)); ?>
        <?php echo $form->error($model, 'Path'); ?>
    </div>

    <div class="row" id="immagini">
        <b><?php echo "Immagini" ?></b>
        <?php
        $this->widget('CMultiFileUpload', array(
            'name' => 'images',
            'accept' => 'jpeg|jpg|gif|png', // useful for verifying files
            //'duplicate' => 'File duplicato!', // useful, i think
            'denied' => 'Estensione non supportata', // useful, i think
            'max' => 10
        ));
        ?>
    </div>
    <div class="row" id="musica">
        <div class="row">
        <?php echo "Tipo Fonte Musicale" ?>
        <?php        
            echo CHtml::dropDownList("TipoFonte", 'File', array(1 => 'File',2 => 'SoundCloud'));       
        ?>        
        </div>
        <div id ="mp3File">
        <b><?php echo "Mp3 Files" ?></b>
        <?php
        $this->widget('CMultiFileUpload', array(
            'name' => 'music',
            'accept' => 'mp3', // useful for verifying files
            //'duplicate' => 'File duplicato!', // useful, i think
            'denied' => 'Estensione non supportata', // useful, i think
            'max' => 10
        ));        
        ?>
        </div>
        <div id="souncloudLink">
            <b><?php echo "Link soundcloud" ?></b>
            <a href="#" id="addTextLink">Add</a>
            <div id="link_inputs">
                <div><input type="text" id="txt_link_1"/><input type="text" id="txt_descr_1"/></div>
            </div>
        </div>
    </div>
    
<!--    <div class="row" id="video">
        <div>
            <b>Aggiungi link</b>
            <table id="linkTable">
                <tr>
                    <th>name</th>
                    <th>link</th>
                    <th>desc</th>

                </tr>
                    <?php 
                        /*if (!$model->IsNewRecord)
                        {
                            $links = $model->Video;
                            foreach($links as $link)
                            {
                                if($link->link != null)
                                {*/
                    ?>
                <tr>
                    <td><?php //echo CHtml::encode($link->title); ?></td>
                    <td><?php //echo CHtml::encode($link->link); ?></td>
                    <td><?php //echo CHtml::encode($link->descr); ?></td>
                    <td>x</td>
                </tr>
                    <?php
                       //         }
                       //     }
                       // }
                       // $arrayVideo = array();
                       // $arrayVideo[] = new Video();
//                        $arrayVideo[] = new Video();
//                        $arrayVideo[] = new Video();
                    ?>
                <tr id="link_1">
                    <td><?php //echo $form->textField($arrayVideo[0], 'title', array('size' => 60, 'maxlength' => 500)); ?></td>
                    <td><?php //echo $form->textField($arrayVideo[0], 'link', array('size' => 60, 'maxlength' => 500)); ?></td>
                    <td><?php //echo $form->textField($arrayVideo[0], 'descr', array('size' => 60, 'maxlength' => 500)); ?></td>   
                    <td></td>
                </tr>
                <tr id="link_2">
                    <td><?php //echo $form->textField($arrayVideo[1], 'title', array('size' => 60, 'maxlength' => 500)); ?></td>
                    <td><?php //echo $form->textField($arrayVideo[1], 'link', array('size' => 60, 'maxlength' => 500)); ?></td>
                    <td><?php //echo $form->textField($arrayVideo[1], 'descr', array('size' => 60, 'maxlength' => 500)); ?></td>   
                    <td></td>
                </tr>
                <tr id="link_3">
                    <td><?php //echo $form->textField($arrayVideo[2], 'title', array('size' => 60, 'maxlength' => 500)); ?></td>
                    <td><?php //echo $form->textField($arrayVideo[2], 'link', array('size' => 60, 'maxlength' => 500)); ?></td>
                    <td><?php //echo $form->textField($arrayVideo[2], 'descr', array('size' => 60, 'maxlength' => 500)); ?></td>   
                    <td></td>
                </tr>
                <?php
                    //$_POST['arrayVideo'] = $arrayVideo;
                ?>
            </table>
            <div style="cursor: pointer;" id="AddLink">Aggiungi</div>
            <input type="button" value="Aggiungi" name="AddLink" onClick="AddRow();" />
        </div>
    </div>-->
<!--<div class="row" id="videofile">



        <b>Aggiungi file</b>
        <?php
//        $this->widget('CMultiFileUpload', array(
//            'name' => 'video',
//            'accept' => 'mpeg|avi|mp4', // useful for verifying files
//            //'duplicate' => 'File duplicato!', // useful, i think
//            'denied' => 'Estensione non supportata', // useful, i think
//            'max' => 10
//        ));
        ?>

    </div>-->


    <div class="row" id="letteratura">
        Letteratura
    </div>
    <div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
    </div>

<?php $this->endWidget(); ?>

</div><!-- form -->